<?php
namespace Member\Controller;
use Org\Yike\Member;
class AccountController extends CommonController {
    public function _initialize(){
        parent::_initialize();
        $this->is_login();
    }

    //我的金额
    public function money(){
        $member = new Member();
        $list = $member::getUserRecord(20,array('type'=>2,'user_id'=>$this->user['id']),true);
        $this->list = $list;
        $this->seoKey(array('seo_title'=>'我的金额'));
        $this->display();
    }

    //我的积分
    public function integral(){
        $member = new Member();
        $list = $member::getUserRecord(20,array('type'=>1,'user_id'=>$this->user['id']),true);
        $this->list = $list;
        $this->seoKey(array('seo_title'=>'我的积分'));
        $this->display();
    }

    //我的消息
    public function message(){
        $member = new Member();
        $list = $member::getUserMessage(20,array('type'=>1,'user_id'=>$this->user['id']),true);
        $this->list = $list;
        $this->seoKey(array('seo_title'=>'我的消息'));
        $this->display();
    }

    //个人信息
    public function info(){
        if($_POST){
            $data = $_POST['info'];
            unset($data['level']);
            unset($data['is_index']);
            $d = $_POST['data'];
            $d['update_time'] = time();
            $data['data'] = serialize($d);
            $data['birthday'] = $d['y'].'-'.$d['m'].'-'.$d['d'];
            //上传图片
            if (!empty($_FILES['img']['name'])) {
                $img = $this->_upload();
                $data['head_ico'] = $img['img']['savepath'].$img['img']['savename'];
            }
            $data= M('user_info')->create($data);
            if(M('user_info')->where(array('user_id'=>$this->user['id']))->save($data)){
                //把用户ID写入分组内
                $users = M ('division')->where (array ('id'=>$data['group']))->getField('users');
                $users .= $users.$data['group'].',';
                M ('division')->where (array ('id'=>$data['group']))-> setField('users', $users);
                $this->success('会员信息修改成功');
            }else{
                $this->error('会员信息修改失败');
            }
        }else{
            $this->binding = M('user_binding')->where(array('user_id'=>$this->user['id']))->find();
            $this->conf = getUserInfoConf();
            $info = M('user_info')->where(array('user_id'=>$this->user['id']))->find();
            $this->data = unserialize($info['data']);
            $this->info = $info;
            $this->seoKey(array('seo_title'=>'个人信息'));
            //所有分组
            $groups = M('division')->select();
            $this->assign ('groups', $groups);
            $this->display();
        }
    }

    //安全设置
    public function security(){
        $this->display();
    }

    //修改密码
    public function password(){
        if($_POST){
            $mobile = I('post.mobile');
            $phone_code = I('post.code');
            $oldPass = I('post.oldPass');
            $newPass = I('post.newPass');
            $newPass1 = I('post.newPass1');
            //普通修改
            if($oldPass){
                $pass = _md5($oldPass);
                if($pass==$this->user['password']){
                    if($newPass == $newPass1){
                        $res = M('user')->where(array('id'=>$this->user['id']))->save(array('password'=>_md5($newPass)));
                        if($res){
                            $this->success('会员密码修改成功',U('Login/logout'));
                        }else{
                            $this->error('会员密码修改失败');
                        }
                    }else{
                        $this->error('新密码再次输入不一致');
                    }
                }else{
                    $this->error('旧的密码输入不正确');
                }
            }

            //手机修改
            if($phone_code){
                $code = session('password_code');
                if(!$code['code']){
                    $this->error('找不到对应的验证码信息');
                }
                if($code['code']!=$phone_code){
                    $this->error('输入的验证码信息不对应');
                }
                if($mobile!=$this->user['mobile']){
                    $this->error('手机号码不对应');
                }

                if($newPass == $newPass1){
                    $res = M('user')->where(array('id'=>$this->user['id']))->save(array('password'=>_md5($newPass)));
                    if($res){
                        session('password_code',null);
                        $this->success('会员密码修改成功',U('Login/logout'));
                    }else{
                        $this->error('会员密码修改失败');
                    }
                }else{
                    $this->error('新密码再次输入不一致');
                }

            }


        }else{
            $this->seoKey(array('seo_title'=>'修改密码'));
            $this->display();
        }
    }

    //绑定邮箱
    public function email(){
        $this->display();
    }

    //意见反馈
    public function opinion(){
        if($_POST){
            $data = $_POST['data'];
            $data['user_id'] = $this->user['id'];
            $data['add_time'] = time();
            $data = M('opinion')->create($data);
            if(M('opinion')->add($data)){
                $this->success("您的反馈信息已收集");
            }else{
                $this->errir("您的反馈信息提交失败");
            }
        }else{
            $this->seoKey(array('seo_title'=>'反馈信息'));
            $this->display();
        }
    }
    //意见反馈列表
    public function opinionList(){
        $member = new Member();
        $list = $member::getOpinionList(20,'status!=4 and user_id='.$this->user['id'],true);
        $this->list = $list;
        $this->seoKey(array('seo_title'=>'反馈信息列表'));
        $this->display();
    }

    //意见反馈删除
    public function opinionDel(){
        $info = M('opinion')->where(array('user_id'=>$this->user['id'],'id'=>intval($_GET['id'])))->find();
        if(!$info){
            $this->error('找不到对应的意见反馈章信息');
        }
        if(M('opinion')->where(array('id'=>$info['id']))->save(array('status'=>4))){
            $this->success('意见反馈删除成功!');
        }else{
            $this->error('意见反馈删除失败');
        }
    }

}